package com.bktech.fragment;

import java.util.ArrayList;

import android.app.Service;
import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bktech.elements.Feed;
import com.bktech.service.ImageDownloader;
import com.bktech.stupidnews.R;

public class ViewFragmentAdapter extends BaseAdapter {

	ArrayList<Feed> listFeed;
	LayoutInflater inflate;

	public ImageDownloader downloader;

	public ViewFragmentAdapter(Context context) {
		listFeed = new ArrayList<Feed>();
		inflate = (LayoutInflater) context
				.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
		downloader = new ImageDownloader(context);
		init();
	}

	public ViewFragmentAdapter(Context context, ArrayList<Feed> list) {
		listFeed = list;
		inflate = (LayoutInflater) context
				.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
		downloader = new ImageDownloader(context);
		init();
	}

	private void init() {

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listFeed.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listFeed.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflate.inflate(R.layout.feed_part, parent, false);
		}
		TextView title = (TextView) convertView.findViewById(R.id.title);
		TextView summary = (TextView) convertView.findViewById(R.id.summary);
		title.setText(listFeed.get(position).title);
		try {
			summary.setText(Html.fromHtml(listFeed.get(position).summary));
		} catch (Exception e) {
			e.printStackTrace();
			Log.d("GetView", "can covert from html");
			summary.setText(listFeed.get(position).summary);
		}
		if (listFeed.get(position).urlImage != null) {
			if (!listFeed.get(position).urlImage.equals("")) {
				ImageView imageView = (ImageView) convertView
						.findViewById(R.id.image);
				downloader.download(listFeed.get(position).urlImage, imageView);
			}
		}
		return convertView;
	}

	public void addFeed(Feed feed) {
		this.listFeed.add(feed);
		this.notifyDataSetChanged();
	}

	public void addFeed(ArrayList<Feed> feeds) {
		for (int i = 0; i < feeds.size(); i++) {
			listFeed.add(feeds.get(i));
		}
		this.notifyDataSetChanged();
	}

	public long getIdNewestFeed() {
		if (listFeed.size() == 0) {
			return 0;
		} else {
			return listFeed.get(0).id;
		}
	}

	public void updateList(ArrayList<Feed> feeds) {
		for (int i = feeds.size() - 1; i >= 0; i--) {
			listFeed.add(0, feeds.get(i));
		}
		notifyDataSetChanged();
	}

	public void removeAllFeed() {
		listFeed = new ArrayList<Feed>();
		notifyDataSetChanged();
	}
}
